package run;

import ga.ComparatorByBestFitness;
import ga.Individual;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Utility {
	
	/* 
	 * Gera um n�mero aleat�rio entre o min e o max
	 */
	public static double between(double min,double max){
		Random random = new Random();
		return min+(max-min) * random.nextDouble();
	}
	
	/* 
	 * Gera um n�mero aleat�rio entre o min e o max
	 */
	public static double betweenGaussian(double min,double max){
		Random random = new Random();
		return min+(max-min) * random.nextGaussian();
	}
	
	/*
	 * Ordena a popula��o em ordem crescente de fitness
	 */
	public static void sortPopulation(ArrayList<Individual> population){
		Collections.sort(population,new ComparatorByBestFitness());
	}

	//roleta
	
	
	public static double fitnessTotal (ArrayList<Individual> pop){
		double retorno = 0;
		for (Individual individual : pop) {
			retorno += 1/individual.getFitness();
		}
		return retorno;
	}

	public static double[] criacaoRoleta (ArrayList<Individual> pop){
		double[] roleta = new double[pop.size()];
		double fitnessTotal = fitnessTotal(pop);
		for (int i = 0; i < pop.size(); i++) {
			roleta[i] = ( 1 / pop.get(i).getFitness()) / fitnessTotal;
		}
		return roleta;

	}


	public static int vencedorRoleta (double[] roleta){
		Random rand = new Random();
		double randomNumber = rand.nextDouble();
		double auxSum = 0;
		int position = -1;

		for (int i = 0; i < roleta.length; i++) {
			auxSum += roleta[i];
			if (randomNumber < auxSum) {
				position = i;
				break;
			}
		}

		return position;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
